/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/
 
package testing;
import lime2.*;
import lights.adapters.*;
import lights.interfaces.*;
import java.util.Random;

/**
 * This testing agent slightly mutates InstSW: each reaction in the chain now also "outs" a specific (unique) tuple 
 * and tries to "inp" exactly that tuple; we are testing whether tuples outed during reactions are "invisible tuples" 
 * or not - an "invisible tuple" is a tuple that is not visible to the listener of the reaction that wrote it. 
 * For this test to be relevant, atomicity of execution of listeners must be guaranteed.
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class InstSWTuple implements Runnable, ReactionListener
{
 // identification number	
 private int id = new Random().nextInt(1000);		
		
 // private tuple-related data members
 private LimeTupleSpace lts = LimeTupleSpace.get("testing");	
 private ITuple strings = new Tuple().add(new Field().setType(String.class));
 private LimeTemplate template = new LimeTemplate(Location.HERE, Location.UNSPECIFIED, strings);
 
 // private int: keeps track whether the last installed reaction was strong or weak
 private int n = 0;
 
 /**
  * Run method.
  */
 public void run()
 {
  System.out.println("InstSWTuple " + id + ": started");	
  Reaction r = new Reaction(Reaction.ONCE, template, this);
  lts.addStrongReaction(new Reaction[] {r});
  System.out.println("InstSWTuple " + id + ": initial strong reaction installed (0)");     
  System.out.println("InstSWTuple " + id + ": dies");
 }	
 
 /**
  * Reactive method.
  * 
  * @param re ReactionEvent
  */
 public void reactsToOut(ReactionEvent re)
 {
  Integer m = new Integer(n);	
  System.out.println("InstSWTuple " + id + ": " + (n % 2 == 0 ? "strong" : "weak") + " reaction fires (" + m + ")");	
  lts.inp(template);
  n++;
  
  // out specific tuple and tries to inp it
  ITuple outuple = new Tuple().add(new Field().setValue(m));
  lts.out(outuple);
  System.out.println("InstSWTuple " + id + ": out of tuple <" + m + ">");
  LimeTuple found = lts.inp(new LimeTemplate(Location.UNSPECIFIED, Location.UNSPECIFIED, outuple));
  
  Reaction r = new Reaction(Reaction.ONCE, template, this);
  if ((n % 2) == 0) { lts.addStrongReaction(new Reaction[] {r}); } else { lts.addWeakReaction(new Reaction[] {r}); }
  System.out.println("InstSWTuple " + id + ": " + ((m.intValue()) % 2 == 0 ? "strong" : "weak") + " reaction ends (" + m + ")");
  System.out.println("InstSWTuple " + id + ": -----------------------------------------> (" + m.intValue() + ") found " + (found == null ? "nothing" : found.getTuple().toString()));
 }

@Override
public void reactsToTake(ReactionEvent re) {
	// TODO Auto-generated method stub
	
}
 	
}
